<?php
namespace app\farm\controller;
use app\ApiRest;

use app\farm\model\Breed;
use app\farm\model\BreedOrder;
use app\farm\model\Claim;
use app\farm\model\ClaimOrder;
use app\farm\model\ClaimText;
use app\farm\model\Config;
use app\farm\model\Evaluate;
use app\farm\model\FinanceWater;
use app\farm\model\LandCate;
use app\farm\model\LandList;
use app\farm\model\LandOrder;
use app\farm\model\LandSourceText;
use app\farm\model\LandSpe;
use app\farm\model\LandText;
use app\farm\model\Machine;
use app\farm\model\Massif;

use app\farm\model\Address;

use app\farm\model\Farmer;
use app\farm\model\Monitor;
use app\farm\model\MonitorText;
use app\farm\model\OrderAddress;
use app\farm\model\Seed;
use app\farm\model\SendOrder;
use app\farm\model\ShopOrder;
use app\farm\model\ShopRefund;
use app\farm\model\Source;
use app\farm\model\SourceText;
use app\farm\model\Wallet;
use app\farm\server\Land;
use think\App;
use think\facade\Db;
use think\Request;


class IndexFarmer extends ApiRest
{

    protected $model;

    protected $address_model;

    protected $massif_model;

    protected $land_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Farmer();

        $cap_dis[] = ['user_id','=',$this->getUserId()];

        $cap_dis[] = ['type','=',1];

        $cap_dis[] = ['status','in',[2,3]];

        $this->farmer = $this->model->dataInfo($cap_dis);

        $action = $this->request->action();

        if(empty($this->farmer)&&!in_array($action,['sourceInfo'])){

            $this->errorMsg('你还不是农场主');
        }

        $this->address_model = new Address();
//
        $this->land_model = new LandList();

        $this->massif_model = new Massif();

//        $this->coupon_record_model = new CouponRecord();

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-24 16:13
     * @功能说明:农场主详情
     */
    public function farmerInfo(){

        return $this->success($this->farmer);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-24 16:16
     * @功能说明:编辑农场主
     */
    public function farmerUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $this->farmer['id']
        ];

        if(isset($input['imgs'])){

            $input['imgs'] = !empty($input['imgs'])?implode(',',$input['imgs']):'';
        }

        if(isset($input['idcard_imgs'])){

            $input['idcard_imgs']  = !empty($input['idcard_imgs'])?implode(',',$input['idcard_imgs']):'';

        }

        $res = $this->model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-10 17:51
     * @功能说明:土地列表
     */
    public function landList(){

        $input = $this->_param;

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $data = $this->land_model->dataList($dis,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-10 18:05
     * @功能说明:添加土地
     */
    public function landAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['farmer_id'] = $this->farmer['id'];

        $res = $this->land_model->dataAdd($input);

        return $this->success($res);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-10 18:05
     * @功能说明:编辑土地
     */
    public function landUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']

        ];

        $input['uniacid'] = $this->_uniacid;

        $res = $this->land_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-10 18:05
     * @功能说明:编辑土地
     */
    public function landStatusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']

        ];

        $res = $this->land_model->where($dis)->update($input);

        return $this->success($res);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-12-10 18:05
     * @功能说明:土地详情
     */
    public function landInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']

        ];

        $data = $this->land_model->landInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 14:43
     * @功能说明:地块列表
     */
    public function massifList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->massif_model->dataList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 14:43
     * @功能说明:地块列表下拉框
     */
    public function massifSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->massif_model->where($dis)->select()->toArray();

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-12-14 14:49
     * @功能说明:添加地块
     */
    public function massifAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['farmer_id'] = $this->farmer['id'];

        $data = $this->massif_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 14:50
     * @功能说明:编辑地块
     */
    public function massifUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;
        //删除
        if(isset($input['status'])&&$input['status']==-1){

            $find = $this->land_model->landSomeFind($input['id'],1);

            if($find==1){

                $this->errorMsg('该地块服务正在被使用');
            }

        }

        $data = $this->massif_model->dataUpdate($dis,$input);

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-12-14 14:50
     * @功能说明:地块详情
     */
    public function massifInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->massif_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-20 16:54
     * @功能说明:种子列表
     */
    public function seedList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $seed_model = new Seed();

        $data = $seed_model->dataList($dis,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-20 16:54
     * @功能说明:种子列表
     */
    public function seedSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $seed_model = new Seed();

        $data = $seed_model->where($dis)->select()->toArray();

        return $this->success($data);

    }
    /**
     * @author chenniang
     * @DataTime: 2021-12-20 16:57
     * @功能说明:添加种子
     */
    public function seedAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['farmer_id'] = $this->farmer['id'];

        $seed_model = new Seed();

        $data = $seed_model->dataAdd($input);

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-12-14 14:50
     * @功能说明:编辑种子
     */
    public function seedUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $seed_model = new Seed();
        //删除
        if(isset($input['status'])&&in_array($input['status'],[-1,0])){

            $find = $this->land_model->landSomeFind($input['id'],2);

            if($find==1){

                $this->errorMsg('该种子正在被使用');
            }

        }

        $data = $seed_model->dataUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 14:50
     * @功能说明:编辑种子
     */
    public function seedInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $seed_model = new Seed();

        $data = $seed_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 15:28
     * @功能说明:认养管理列表
     */
    public function claimList(){

        $input = $this->_param;

        $claim_model = new Claim();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $claim_model->dataList($dis,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:32
     * @功能说明:添加认养
     */
    public function claimAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['farmer_id'] = $this->farmer['id'];

        $claim_model = new Claim();

        $res = $claim_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:32
     * @功能说明:认养详情
     */
    public function claimInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $claim_model = new Claim();

        $res = $claim_model->dataInfo($dis);

        $claim_text_model  = new ClaimText();

        $monitor_text_model= new MonitorText();

        $monitor_model     = new Monitor();
        //流程
        $res['process'] = $claim_text_model->where(['claim_id'=>$input['id']])->select()->toArray();
        //监控
        $res['monitor'] = $monitor_text_model->where(['obj_id'=>$input['id'],'type'=>2])->column('monitor_id');

        $res['monitor'] = array_values($res['monitor']);

        $res['monitor'] = $monitor_model->where('id','in',$res['monitor'])->where(['status'=>1])->select()->toArray();

        $cate_model = new LandCate();
        //分类名字
        $res['cate_name'] = $cate_model->where(['id'=>$res['cate_id'],'type'=>2])->value('title');

        $source_model = new Source();
        //溯源名字
        $res['source_name'] = $source_model->where(['id'=>$res['source_id']])->value('title');

        $mac_model = new Machine();
        //仪器名字
        $res['machine_name'] = $mac_model->where(['id'=>$res['machine_id'],'status'=>1])->value('title');

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:32
     * @功能说明:编辑认养
     */
    public function claimUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $claim_model = new Claim();

        $res = $claim_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 16:32
     * @功能说明:编辑认养
     */
    public function claimStatusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $claim_model = new Claim();

        $res = $claim_model->where($dis)->update($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 17:04
     * @功能说明:溯源列表
     */
    public function sourceList(){

        $input = $this->_param;

        $source_model = new Source();

//        $source_text_model = new SourceText();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $data = $source_model->dataList($dis,10);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 17:04
     * @功能说明:溯源下拉框
     */
    public function sourceSelect(){

        $input = $this->_param;

        $source_model = new Source();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $data = $source_model->where($dis)->select()->toArray();

        return $this->success($data);
    }

    /**
     * @author chenniang
     * @DataTime: 2021-12-14 17:10
     * @功能说明:添加溯源
     */
    public function sourceAdd(){

        $input = $this->_input;

        $source_model = new Source();

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['farmer_id'] = $this->farmer['id'];

        $res = $source_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 17:12
     * @功能说明:编辑溯源
     */
    public function sourceUpdate(){

        $input = $this->_input;

        $source_model = new Source();

        $input['uniacid'] = $this->_uniacid;

        $dis = [

            'id' => $input['id']
        ];
        //删除
        if(isset($input['status'])&&$input['status']==-1){

            $find = $this->land_model->landSomeFind($input['id'],3);

            if($find==1){

                $this->errorMsg('该溯源正在被使用');
            }

        }

        $res = $source_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 17:12
     * @功能说明:编辑溯源
     */
    public function sourceInfo(){

        $input = $this->_param;

        $source_model = new Source();

        $dis = [

            'id' => $input['id']
        ];

        $res = $source_model->dataInfo($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-29 10:29
     * @功能说明:养殖管理
     */
    public function breedList(){

        $input = $this->_param;

        $breed_model = new Breed();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $data = $breed_model->dataList($dis,10);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-29 10:30
     * @功能说明:添加养殖管理
     */
    public function breedAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['farmer_id'] = $this->farmer['id'];

        $breed_model = new Breed();

        $res = $breed_model->dataAdd($input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-29 10:34
     * @功能说明:编辑养殖管理
     */
    public function breedUpdate(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $dis = [

            'id' => $input['id']
        ];

        $breed_model = new Breed();

        $res = $breed_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-29 10:34
     * @功能说明:编辑养殖管理
     */
    public function breedInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $breed_model = new Breed();

        $res = $breed_model->dataInfo($dis);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-30 11:15
     * @功能说明:申请提现
     */
    public function applyWallet(){

        $input = $this->_input;

        $wallet_model = new Wallet();

        $config_model = new Config();

        $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        if($input['apply_price']>$this->farmer['cash']){

            $this->errorMsg('余额不足');
        }

        Db::startTrans();

        $insert = [

            'uniacid'    => $this->_uniacid,

            'user_id'    => $this->getUserId(),

            'order_code' => orderCode(),

            'farmer_id'  => $this->farmer['id'],

            'pay_price'  => $input['apply_price'],

            'text'       => $input['text'],

            'true_price' => round($input['apply_price']*$config['cash_balance']/100,2),

            'balance'    => $config['cash_balance'],
        ];
        //发起提现
        $res = $wallet_model->dataAdd($insert);

        if($res==0){

            Db::rollback();

            $this->errorMsg('申请失败');

        }

        $water_model = new FinanceWater();

        $id = $wallet_model->getLastInsID();
        //添加拒绝提现记录
        $res = $water_model->addWater($id,7,1,1);

        if($res==0){

            Db::rollback();

            $this->errorMsg('申请失败');

        }
        //执行记录
        $res = $water_model->cashArrival($this->farmer['id']);

        if($res==0){

            Db::rollback();

            $this->errorMsg('申请失败');

        }

        Db::commit();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 15:06
     * @功能说明:农场主财务详情
     */
    public function farmerFinanceInfo(){

        $water_model = new FinanceWater();

        $water_model->cashArrival($this->farmer['id']);
        //余额
        $data['cash'] = $this->model->where(['id'=>$this->farmer['id']])->value('cash');

        $water_data = $water_model->getDayCash($this->farmer['id']);
        //总金额
        $data['total_cash'] = $water_data['income_cash'];
        //冻结金额
        $data['frozen_cash']= $water_model->where(['farmer_id'=>$this->farmer['id'],'add'=>1,'role_type'=>1])->where('cash_status','in',[0,1])->sum('price');

        $data['frozen_cash']= round($data['frozen_cash'],2);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 16:35
     * @功能说明:农场主流水记录
     */
    public function farmerFinanceList(){

        $input = $this->_param;

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $dis[] = ['role_type','=',1];

        if(isset($input['add'])){

            $dis[] = ['add','=',$input['add']];
        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        $water_model = new FinanceWater();

        $data = $water_model->dataList($dis,10);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-30 17:09
     * @功能说明:农场主提现列表
     */
    public function walletList(){

        $input = $this->_param;

        $dis = [

            'farmer_id' => $this->farmer['id'],

            'is_show'   => 1,

            'uniacid'   => $this->_uniacid

        ];

        if(!empty($input['status'])){

            $dis['status'] = $input['status'];
        }

        $wallet_model = new Wallet();

        $data = $wallet_model->dataList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-31 15:11
     * @功能说明:认养和土地分类
     */
    public function landAndClaimCate(){

        $input = $this->_param;

        $dis = [

            'a.status' => 1,

            'a.uniacid'=> $this->_uniacid,

            'a.type'   => $input['type']
        ];

        $where = [

            'a.is_public' => 1,

            'b.farmer_id' => $this->farmer['id']
        ];

        $cate_model = new LandCate();

        $data = $cate_model->alias('a')
                ->join('lbfarm_land_cate_text b','a.id = b.cate_id','left')
                ->where($dis)
                ->where(function ($query) use ($where){
                    $query->whereOr($where);
                })
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->select()
                ->toArray();

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-01-10 15:52
     * @功能说明:养殖订单列表
     *
     */

    public function breedOrderList(){

        $input = $this->_param;

        $dis[] = ['pay_type','>',1];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $order_model = new BreedOrder();

        $data = $order_model->dataList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-09 15:16
     * @功能说明:认养订单
     */
    public function claimOrderList(){

        $input = $this->_param;

        $order_model = new ClaimOrder();

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        if(!empty($input['pay_type'])){

            $dis[] = ['pay_type','=',$input['pay_type']];

        }else{

            $dis[] = ['pay_type','>',-1];

        }

        $data = $order_model->dataList($dis);

        if(!empty($data['data'])){

            $address_model  = new OrderAddress();

            foreach ($data['data'] as &$v){
                //地址信息
                $v['address_info'] = $address_model->dataInfo(['order_id'=>$v['id'],'type'=>2]);
            }
        }

        $arr = [
            //认养中
            2 => 'claim_ing',
            //配送中
            3 => 'send_ing'
        ];

        foreach ($arr as $k=>$value){

            $dis = [

                'pay_type' => $k,

                'farmer_id'=> $this->farmer['id']
            ];

            $data[$value] = $order_model->where($dis)->where('pay_time','>',0)->count();
        }

        return $this->success($data);


    }



    /**
     * @author chenniang
     * @DataTime: 2022-02-09 09:47
     * @功能说明:发货订单发货
     */
    public function sendOrderSend(){

        $input = $this->_input;

        $send_order_model = new SendOrder();

        $send_order = $send_order_model->dataInfo(['id'=>$input['id']]);

        if(empty($send_order)){

            $this->errorMsg('订单未找到');
        }

        if($send_order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }

        $update = [

            'pay_type' => 3,

            'send_time'=> time(),

            'auto_receiving_time' => $this->_config['auto_hx_time']*86400+time()

        ];

        Db::startTrans();

        $res = $send_order_model->dataUpdate(['id'=>$input['id']],$update);

        if($res==0){

            Db::rollback();

            $this->errorMsg('发货失败');
        }

        if($send_order['type']==1){

            $claim_order_model = new ClaimOrder();

            $claim_order_model->dataUpdate(['id'=>$send_order['order_id']],['pay_type'=>3]);
        }

        Db::commit();

        return $this->success(true);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-02-08 17:14
     * @功能说明:配送订单列表
     */
    public function farmerSendOrderList(){

        $input = $this->_param;

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $dis[] = ['pay_time','>',0];

        if(!empty($input['pay_type'])){

            $dis[] = ['pay_type','=',$input['pay_type']];
        }

        $send_order_model = new SendOrder();

        $data = $send_order_model->dataList($dis);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['time_text'] = date('Y-m-d H:i',$v['start_time']).'~'.date('H:i',$v['end_time']);

                $v['create_time']    = date('Y-m-d H:i:s',$v['create_time']);

                $v['pay_time']       = $v['pay_time']>0?date('Y-m-d H:i:s',$v['pay_time']):0;

                $v['send_time']      = $v['send_time']>0?date('Y-m-d H:i:s',$v['send_time']):0;

                $v['receiving_time'] = $v['receiving_time']>0?date('Y-m-d H:i:s',$v['receiving_time']):0;

                $v['refund_time']    = $v['refund_time']>0?date('Y-m-d H:i:s',$v['refund_time']):0;
            }
        }

        $arr = [
            //待配送
            2 => 'no_send',
            //配送中
            3 => 'send_ing'
        ];

        foreach ($arr as $k=>$value){

            $dis = [

                'pay_type' => $k,

                'farmer_id'=> $this->farmer['id']
            ];

            $data[$value] = $send_order_model->where($dis)->where('pay_time','>',0)->count();
        }

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-02-09 09:47
     * @功能说明:配送订单收货
     */
    public function sendOrderPickup(){

        $input = $this->_input;

        $send_order_model = new SendOrder();

        $send_order = $send_order_model->dataInfo(['id'=>$input['id']]);

        if(empty($send_order)){

            $this->errorMsg('订单未找到');
        }

        if($send_order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }

        $res = $send_order_model->sendOrderReceiving($send_order);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-28 11:26
     * @功能说明:用户下单列表
     */
    public function landOrderList(){

        $input = $this->_param;

        $order_model  = new LandOrder();

        $farmer_model = new Farmer();

        $order_model->orderInit($this->getUserId());

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        if(!empty($input['pay_type'])){

            $dis[] = ['pay_type','=',$input['pay_type']];

        }else{

            $dis[] = ['pay_type','>',-1];

        }

        if(!empty($input['order_code'])){

            $dis[] = ['order_code','like','%'.$input['order_code'].'%'];

        }

        $data = $order_model->dataList($dis);

        if(!empty($data['data'])){

            $address_model  = new OrderAddress();

            foreach ($data['data'] as &$v){

                $v['farmer_info'] = $farmer_model->dataInfo(['id'=>$v['farmer_id']],'title');

                $v['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
                //地址信息
                $v['address_info'] = $address_model->dataInfo(['order_id'=>$v['id'],'type'=>1]);

            }
        }

        $arr = [1=>'no_pay_count',2=>'rent_count',3=>'send_count'];

        foreach ($arr as $key => $value){

            $where = [

                'farmer_id' => $this->farmer['id'],

                'pay_type'  => $key
            ];

            $data['count'][$value] = $order_model->where($where)->count();

        }

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-02-21 11:00
     * @功能说明:仪器列表
     */
    public function machineSelect(){

        $input = $this->_param;

        $mac_model = new Machine();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $data = $mac_model->where($dis)->select()->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-22 17:11
     * @功能说明:生成二维码
     */
    public function getSourceQr(){

        $input = $this->_input;

        $uniacid = $this->_uniacid;

        $key = 'sourcekey'.$input['id'];

        $qr  = getCache($key,$uniacid);

        if(empty($qr)||!empty($input['is_update'])){

            $data = longbingCreateWxCode($uniacid,$input,$input['page']);

            $data = transImagesOne($data ,['qr_path'] ,$uniacid);

            $qr   = $data['qr_path'];
        }

        return $this->success($qr);

    }






    /**
     * @author chenniang
     * @DataTime: 2022-03-01 11:03
     * @功能说明:订单列表
     */
    public function shopOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.farmer_id','=',$this->farmer['id']];

        $dis[] = ['a.farmer_show','=',1];

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.goods_name','like','%'.$input['name'].'%'];

            $where[] = ['a.order_code','like','%'.$input['name'].'%'];

        }

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }else{

            $dis[] = ['a.pay_type','>',1];

        }

        $order_model = new ShopOrder();

        $data = $order_model->indexDataList($dis,$where);

        $arr = [
            //未发货
            'no_send_count'  => 2,
            //已经发货
            'have_send_count'=> 3
        ];

        foreach ($arr as $ks=>$vs){

            $map = [

                'farmer_id'=> $this->farmer['id'],

                'pay_type'=> $vs,

                'farmer_show' => 1
            ];

            $data[$ks] = $order_model->where($map)->count();
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:58
     * @功能说明:订单详情
     */
    public function shopOrderInfo(){

        $input = $this->_param;


        $dis = [

            'id' => $input['id']
        ];

        $order_model = new ShopOrder();

        $data = $order_model->dataInfo($dis);

        $data['over_time'] -= time();

        $time_arr = ['create_time','pay_time','end_time','hx_time','over_time','cancel_time'];

        foreach ($time_arr as $v){

            $data[$v] = !empty($data[$v])?date('Y-m-d H:i:s',$data[$v]):$data[$v];

        }

        $data['distance'] = distance_text($data['distance']);

        $end_time = date('H:i',$data['send_end_time'])=='00:00'?'24:00':date('H:i',$data['send_end_time']);
        //配送时间
        $data['user_send_time'] = date('Y-m-d H:i',$data['send_start_time']).'~'.$end_time;

        $farmer_model = new Farmer();

        $data['farmer_info'] = $farmer_model->dataInfo(['id'=>$data['farmer_id']],'title');

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-07 11:08
     * @功能说明:地主商城订单发货
     */
    public function shopOrderSend(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $order_model = new ShopOrder();

        $order = $order_model->dataInfo($dis);

        if(empty($order)||$order['pay_type']!=2){

            $this->errorMsg('订单状态错误');
        }
        $refund_model = new ShopRefund();
        //判断有无申请中的退款订单
        $refund_order = $refund_model->dataInfo(['order_id'=>$input['id'],'status'=>1]);

        if(!empty($refund_order)){

            $this->errorMsg('该订单正在申请退款，请先处理再核销');

        }

        $config_model = new Config();

       // $config = $config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $update = [

            //'pay_type'        => 3,

//            'express_company' => $input['express_company'],
//
//            'express_code'    => $input['express_code'],
//
//            'express_mobile'  => $input['express_mobile'],
//
//            'express_user'    => $input['express_user'],

           // 'send_time'       => time(),

            'video'           => $input['video'],

            //'hx_over_time'    => time()+$config['auto_hx_time']*86400,

        ];

        $data = $order_model->dataUpdate($dis,$update);

//        $water_model = new FinanceWater();
//        //商城订单农场主获得运费
//        $water_model->addWater($order['id'],16,1,0);

//        $order = $order_model->dataInfo($dis);
//
//        $order_model->sendOrderService($order);

        return $this->success($data);


    }



    /**
     * @author chenniang
     * @DataTime: 2022-04-11 14:22
     * @功能说明:监控列表
     */
    public function monitorSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $dis[] = ['farmer_id','=',$this->farmer['id']];

        $monitor_model = new Monitor();

        $data = $monitor_model->where($dis)->order('id desc')->select()->toArray();

        return $this->success($data);

    }






}
